<?php
/**
 * 描述：新闻数据访问类。
 * @final
 */
final class DAO_News
{
    /**
     * 描述：获取最新新闻数据。
     * 说明：筛除掉新闻类别中的"新闻专题"的信息。
     * @static
     */
    public static function Latest($size=10)
    {        
        $data=DB::GetRows('select * from news where M_IsDelete=FALSE and ClassID<>'.DAO_NewsClass::Topic_ID.' order by ID desc limit 0,'.$size);
        return $data;
    }
    
    /**
     * 描述：获取热点排行新闻数据。
     * 说明：信息来自航运、海工、船厂、船配大类，并按最新发布顺序排序。
     * @static
     */
    public static function Ranking($size=10)
    {        
        $data=DB::GetRows('select * from news where M_IsDelete=FALSE and (ClassID='.DAO_NewsClass::SHIPYARD_ID.' or ClassID='.DAO_NewsClass::ANCILLARY_ID.' or ClassID='.DAO_NewsClass::SHIPPING_ID.' or ClassID='.DAO_NewsClass::MARITIMEWORKS_ID.') order by ID desc limit 0,'.$size);
        if($data)
        {
            foreach ($data as &$row)
            {
                $row['M_HTMLFile']=Config::PATH_RELATIVE_CONTENT_NEWS.'/'.$row['M_HTMLFile'];
            }
        }
        return $data;
    }

    /**
     * 描述：获取“置顶性质”指定小类数据。
     * @static
     */
    public static function Area_Subclass($classID,$sizeCommon,$sizeThumbnail)
    {
        $tmp=array();
        $areaCount=$sizeCommon;
        $data=DB::GetRows('select * from news where SubclassID='.$classID.' and M_IsDelete=FALSE and M_IsTop=TRUE order by M_IsTop desc,M_IsTop_Datetime desc,ID desc limit 0,1');
        if($data)
        {
            $data=$data[0];
            $data['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$data['Thumbnail'];
            $data['Content']=strip_tags($data['Content']);
            $areaCount=$sizeThumbnail;
        }
        $tmp['Topside']=$data;

        $data=DB::GetRows('select * from news where '.($data==NULL ? '1=1':'ID<>'.$data['ID']).' and SubclassID='.$classID.' and M_IsDelete=FALSE order by ID desc limit 0,'.$areaCount);
        if($data)
        {
            foreach($data as &$row)
            {
                if((time()-strtotime($row['Datetime']))/3600/24<=1)
                    $row['IsNew']='TRUE';
                else
                    $row['IsNew']='FALSE';
            }
        }
        $tmp['Foreach']=$data;
        return $tmp;
    }

    /**
     * 描述：获取“置顶性质”指定大类数据。
     * @static
     */
    public static function Area_Class($classID,$classID_Topside,$sizeCommon,$sizeThumbnail)
    {
        $tmp=array();
        $areaCount=$sizeCommon;
        $data=DB::GetRows('select * from news where SubclassID='.$classID_Topside.' and M_IsDelete=FALSE and M_IsTop=TRUE order by M_IsTop desc,M_IsTop_Datetime desc,ID desc limit 0,1');
        if($data)
        {
            $data=$data[0];
            $data['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$data['Thumbnail'];
            $data['Content']=strip_tags($data['Content']);
            $areaCount=$sizeThumbnail;
        }
        $tmp['Topside']=$data;

        $data=DB::GetRows('select * from news where '.($data==NULL ? '1=1':'ID<>'.$data['ID']).' and ClassID='.$classID.' and M_IsDelete=FALSE order by ID desc limit 0,'.$areaCount);
        if($data)
        {
            foreach($data as &$row)
            {
                if((time()-strtotime($row['Datetime']))/3600/24<=1)
                    $row['IsNew']='TRUE';
                else
                    $row['IsNew']='FALSE';
            }
        }
        $tmp['Foreach']=$data;
        return $tmp;
    }

    /**
     * 描述：获取指定大类数据，默认排序。
     * @static
     */
    public static function Class_Default($classID,$count=5)
    {
        if(!is_numeric($classID)){
            return NULL;
        }
        return DB::GetRows('select * from news where ClassID='.$classID.' order by ID desc limit 0,'.$count);
    }

    /**
     * 描述：获取指定小类数据，默认排序。
     * @static
     */
    public static function Subclass_Default($classID,$count=5)
    {
        if(!is_numeric($classID)){
            return NULL;
        }
        return DB::GetRows('select * from news where SubclassID='.$classID.' order by ID desc limit 0,'.$count);
    }
}
?>